//
//  TZLoginViewController.m
//  XMLYFM
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 zzl. All rights reserved.
//

#import "TZLoginViewController.h"
#import "ZHTabBarController.h"
#import "TZzhuceViewController.h"
#import "CoreDataManager.h"
#import "User.h"


//测试
#import "TZzhuceinfo.h"

@interface TZLoginViewController ()
@property(nonatomic,retain)NSManagedObjectContext *managerObjectContext;
@property(nonatomic,retain)NSMutableArray *resultArr;//搜索结果
@property(nonatomic,copy)NSString *mima;
@property(nonatomic,copy)NSString *myname;
@end

@implementation TZLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"登陆";
    
    self.zhangHao.layer.borderColor = [UIColor colorWithRed:0.77 green:0.77 blue:0.77 alpha:1].CGColor;
    self.zhangHao.layer.borderWidth = 1;
    self.miMa.layer.borderColor = [UIColor colorWithRed:0.77 green:0.77 blue:0.77 alpha:1].CGColor;
    self.miMa.layer.borderWidth = 1;
 
    self.view.backgroundColor = [UIColor whiteColor];
    _managerObjectContext = [[CoreDataManager shareManager]managedObjectContext];
//        NSLog(@"🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶🐶%@",[[CoreDataManager shareManager]applicationDocumentsDirectory]);
    self.zhuce.layer.borderColor = [UIColor orangeColor].CGColor;
    self.zhuce.layer.borderWidth = 1;
    self.denglu.backgroundColor = [UIColor colorWithRed:0.97 green:0.39 blue:0.26 alpha:1];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)zhuceAction:(UIButton *)sender {
    TZzhuceViewController *tzZhuCe = [[TZzhuceViewController alloc]init];
    [self.navigationController pushViewController:tzZhuCe animated:YES];
    
//    TZzhuceinfo *zhuce = [[TZzhuceinfo alloc]init];
//    [self.navigationController pushViewController:zhuce animated:YES];
    
    
}
- (IBAction)dengluAction:(UIButton *)sender
{
    if (self.zhanghao.text.length == 0 || self.mimaFiled.text.length == 0)
    {
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"账号或密码不能为空" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertC animated:YES completion:^{
            
            
        }];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
            [self.navigationController dismissViewControllerAnimated:YES completion:^{
                
                
            }];
            
        }];
        
        [alertC addAction:action];

    }
    //账号或者密码不为空

    else
    {
    
        //查询请求
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        
        //获取要查询的实体的描述
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:_managerObjectContext];
        
        //对fetchRequest 设置查询的实体
        [fetchRequest setEntity:entity];
        //第一个参数:按那个字段排序
        //第二个参数 是否升序的意思
        
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name"
                                                                       ascending:YES];
        [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
        
        NSError *error = nil;
        //从数据库中搜索满足上面条件的元素
        NSArray *fetchedObjects = [_managerObjectContext executeFetchRequest:fetchRequest error:&error];
        if (fetchedObjects == nil) {
            NSLog(@"🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱🐱%@",error);
        }
        
        _resultArr = [NSMutableArray arrayWithArray:fetchedObjects];
        
        
        for (User *user in _resultArr)
        {
            if ([user.phoneNumber isEqualToString:self.zhanghao.text]) {
                self.mima = user.mima;
                self.myname = user.name;
            }
            break;
        
        }
    
        
        
        if ([self.mima isEqualToString:self.mimaFiled.text])
        {
            [self.navigationController popToRootViewControllerAnimated:YES];
            [[NSUserDefaults standardUserDefaults]setValue:@"1" forKey:@"logon"];
            [[NSUserDefaults standardUserDefaults]setValue:self.myname forKey:@"person"];
            [self.navigationController popToRootViewControllerAnimated:YES];
            
        }
      else
      {
      
          UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码错误" preferredStyle:UIAlertControllerStyleAlert];
          [self presentViewController:alertC animated:YES completion:^{
              
              
          }];
          UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
              
              [self.navigationController dismissViewControllerAnimated:YES completion:^{
                  
                  
              }];
              
          }];
          
          [alertC addAction:action];

      
      }
    
   
    }
    
    
}
@end
